<?php
    error_reporting(0);
    require_once('../model/PdoMySQL.class.php');
    require_once('../model/config.php');
    header("Content-Type: text/html;charset=utf-8");
    if($_SERVER['REQUEST_METHOD']=="POST"){
      run();
    }

    function run(){
        $cageid=$_REQUEST['cageid'];
        $re=$_REQUEST['re'];
        if($re=='1'){
            $longjiaid=$_REQUEST['longid'];
            $num=$_REQUEST['num'];
            $temp=intval($num);
            $d=((($temp-1) / 9) + 65);
            $bh=$longjiaid.$d.(($temp-1) % 9 + 1);
            $pdo=new PdoMySQL();
            $sql1="select * from cageapply  where  cageid =?";
            $stmt1=$pdo->prepare($sql1);
            $stmt1->execute(array($cageid));

            while($row1=$stmt1->fetch()){
                $table="cage";
                $username=$row1['2'];
                $animalsum=$row1['number'];
                $pdo2=new PdoMySQL();
                $sql2="insert into cage (cageid,username,number,animalsum,formerid) values('".$bh."','".$username."','".$num."','".$animalsum."','".$cageid."')";
                $stmt2=$pdo2->prepare($sql2);
                $lastInsertId=$pdo->getLastInsertId();
                if($lastInsertId){
                    $sql3="update cageapply set status = 1  where cageid = '{$cageid}';";
                    $stmt3=$pdo->query($sql3);
                    if($stmt3==1){
                        echo '<script language="JavaScript">window.location.href="../view/applycheck.php";</script>';
                    }
                }
            }
        }else if($re=="2"){
            $mes=$_REQUEST['message'];
            $pdo3=new PdoMySQL();

            $sql4="update cageapply set status = 2 , message ='".$mes."' where cageid = '{$cageid}';";
            $stmt4=$pdo3->query($sql4);
            if($stmt4==1){

            }

        }
    }
?>